//
//  SNCompilePlanSectionHeaderView.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/29.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNAddFertilizerInformationController.h"
#import "SNAddPesticideInformationController.h"
#import "SNFertilizerInformationController.h"
#import "SNPesticideInformationController.h"
#import "SNSummaryInfoViewController.h"
#import "UIImageView+TapGesture.h"
#import "SNCompilePlanSectionHeaderView.h"
#import "SNPlanListLabel.h"
#import "SNSeedViewController.h"
@interface SNCompilePlanSectionHeaderView()
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageViewHeightLayoutConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageViewWidthLayoutConstraint;

@property (weak, nonatomic) IBOutlet SNPlanListLabel *rightLabel;
@end
@implementation SNCompilePlanSectionHeaderView
- (void)configCellWithTitle:(NSString *)title imageName:(NSString *)imageName productionPlanListResult:(SNProductionPlanListResult *)productionPlanListResult saveButtonCurrentTitle:(NSString *)saveButtonCurrentTitle{
    _productionPlanListResult = productionPlanListResult;
    
    self.iconImageView.image = [UIImage imageNamed:imageName];
    
    self.leftLabel.text = [NSString stringWithFormat:@"* %@",title];
    
    if([self.leftLabel.text containsString:@"概要信息"]){
        self.rightLabel.text = @"编辑";
        self.leftLabel.textColor = HexRGB(0x67B245);
        UIImage * image = self.iconImageView.image;
        self.iconImageViewWidthLayoutConstraint.constant = 20;
        self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;
    }else if ([self.leftLabel.text containsString:@"种子信息"]){
        self.rightLabel.text = @"编辑";
        self.leftLabel.textColor = HexRGB(0xF49E24);
        UIImage * image = self.iconImageView.image;
        self.iconImageViewWidthLayoutConstraint.constant = 20;
        self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;
    }else if ([self.leftLabel.text containsString:@"肥料信息"]){
        self.rightLabel.text = @"添加";
        self.leftLabel.text = [NSString stringWithFormat:@" * %@",title];
        self.leftLabel.textColor = HexRGB(0x04177A);
        UIImage * image = self.iconImageView.image;
        self.iconImageViewWidthLayoutConstraint.constant = 15;
        self.iconImageViewHeightLayoutConstraint.constant = 15 * image.size.height / image.size.width;
    }else if ([self.leftLabel.text containsString:@"农药信息"]){
        self.rightLabel.text = @"添加";
        self.leftLabel.textColor = HexRGB(0x078F91);
        self.leftLabel.text = [NSString stringWithFormat:@" %@",title];
        UIImage * image = self.iconImageView.image;
        self.iconImageViewWidthLayoutConstraint.constant = 20;
        self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;
    }
    [SNUtils specialLabelWithstring:self.leftLabel.text keyString:@"*" label:self.leftLabel color:HexRGB(0xFF5D04)];
    
    
    
    @weakify_self
    self.rightLabel.sn_tapBlock = ^(){
        @strongify_self
        NSLog(@"%@ | %s",self.class,__func__);
        
        if([self.leftLabel.text containsString:@"概要信息"]){
            SNSummaryInfoViewController * vc = [SNSummaryInfoViewController new];
            SNProductionPlanListResult * productionPlanListResult = self.productionPlanListResult;
            vc.productionPlanListResult = productionPlanListResult;
            [self.nav pushViewController:vc animated:YES];
        }else if ([self.leftLabel.text containsString:@"种子信息"]){
            @weakify_self
            [self checkWithBlock:^{
                @strongify_self
                SNSeedViewController * vc = [SNSeedViewController new];
                SNProductionPlanListResult * productionPlanListResult = self.productionPlanListResult;
                vc.productionPlanListResult = productionPlanListResult;
                [self.nav pushViewController:vc animated:YES];
            }];
            
            //            if (self.productionPlanListResult.cropTypeName.length > 0) {
            //                SNSeedViewController * vc = [SNSeedViewController new];
            //                SNProductionPlanListResult * productionPlanListResult = self.productionPlanListResult;
            //                vc.productionPlanListResult = productionPlanListResult;
            //                [self.nav pushViewController:vc animated:YES];
            //            }else{
            //                [self addAlertViewController];
            //            }
            
        }else if ([self.leftLabel.text containsString:@"肥料信息"]){
            @weakify_self
            [self checkWithBlock:^{
                @strongify_self
                SNAddFertilizerInformationController * vc = [SNAddFertilizerInformationController new];
                [self.nav pushViewController:vc animated:YES];
            }];
        }else if ([self.leftLabel.text containsString:@"农药信息"]){
            @weakify_self
            [self checkWithBlock:^{
                @strongify_self
                SNAddPesticideInformationController * vc = [SNAddPesticideInformationController new];
                [self.nav pushViewController:vc animated:YES];
            }];
        }
        
    };
    
    if ([saveButtonCurrentTitle isEqualToString:@"提交审批"]) {
        self.rightLabel.hidden = YES;
    }else{
        self.rightLabel.hidden = NO;
    }
}
- (void)checkWithBlock:(void(^)())block {
    
    if (self.productionPlanListResult.cropTypeName.length > 0) {
        !block ?: block();
    }else{
        [self addAlertViewController];
    }
    
}
- (void)addAlertViewController
{
    NSString * title = @"";
    NSString * message = @"请先编辑概要信息!";
    NSString * actionButtonTitle = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:actionButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        
    }];
    
    [alertController addAction:action];
    
    [self.nav presentViewController:alertController animated:YES completion:nil];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [SNUtils specialLabelWithstring:self.leftLabel.text keyString:@"*" label:self.leftLabel color:HexRGB(0xFF5D04)];
    
    
    UIImage * image = self.iconImageView.image;
    
    self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;
    
    @weakify_self
    self.rightLabel.sn_tapBlock = ^(){
        @strongify_self
        NSLog(@"%@ | %s",self.class,__func__);
        
        if([self.leftLabel.text containsString:@"概要信息"]){
            SNSummaryInfoViewController * vc = [SNSummaryInfoViewController new];
            [self.nav pushViewController:vc animated:YES];
        }else if ([self.leftLabel.text containsString:@"种子信息"]){
            SNSeedViewController * vc = [SNSeedViewController new];
            [self.nav pushViewController:vc animated:YES];
        }else if ([self.leftLabel.text containsString:@"肥料信息"]){
            SNFertilizerInformationController * vc = [SNFertilizerInformationController new];
            [self.nav pushViewController:vc animated:YES];
        }else if ([self.leftLabel.text containsString:@"农药信息"]){
            SNPesticideInformationController * vc = [SNPesticideInformationController new];
            [self.nav pushViewController:vc animated:YES];
        }
        
    };
}

@end

